package com.suitsoft.ut.service.info;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.PropertyFilter.MatchType;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.dao.account.UserDao;
import com.suitsoft.ut.dao.info.InfoDao;
import com.suitsoft.ut.entity.account.User;
import com.suitsoft.ut.entity.base.College;
import com.suitsoft.ut.entity.info.Info;
import com.suitsoft.ut.service.ServiceException;

/**
 * 安全相关实体的管理类, 包括用户,角色,资源与授权类.
 * 
 * @author calvin
 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class InfoManager {

	private static Logger logger = LoggerFactory.getLogger(InfoManager.class);
@Autowired
	private InfoDao infoDao;

    @Autowired
    private UserDao userDao;
    private User user;	

	//-- User Manager --//
	@Transactional(readOnly = true)
	public Info getInfo(Long id) {
		return infoDao.get(id);
	}
	
	public List<Info> getAll(){
		return infoDao.getAll();
	}
	
	
	
	public void saveInfo(Info entity) {
		//String webpath = ServletActionContext.getServletContext().getRealPath("WEB-INF/files");
		user = userDao.get(UserHolder.getLoginUser().getId());
		entity.setLastUser(user);
		entity.setInfoTime(new Date());
		//String filename1 = entity.getInfoAttachmentName();
		
		//entity.setInfoAttachmentName(filename1.substring(filename1.lastIndexOf(".")));
				
		infoDao.save(entity);
	}

	/**
	 * 删除信息
	 */
	public void deleteInfo(Long id) {
		
		infoDao.delete(id);
	}

	/**
	 * 使用属性过滤条件查询用户.
	 */
	@Transactional(readOnly = true)
	public Page<Info> searchInfo(final Page<Info> page, final List<PropertyFilter> filters) {
		return infoDao.findPage(page, filters);
	}

	@Transactional(readOnly = true)
	public Info findInfoByTitle(String infoTitle) {
		return infoDao.findUniqueBy("infoTitle", infoTitle);
	}
	

}
